<template>
  <div class="table-box">
    <ProTable ref="proTable" :columns="columns" :request-api="getDictDataList" :init-param="initParam" row-key="dictCode">
      <!-- 表格 header 按钮 -->
      <template #tableHeader>
        <el-button v-auth="'dict_data_save'" type="primary" :icon="CirclePlus" @click="openDrawer('新增')">新增字典值</el-button>
      </template>
      <!-- 表格操作 -->
      <template #operation="scope">
        <el-button v-auth="'dict_data_save'" type="primary" link :icon="EditPen" @click="openDrawer('编辑', scope.row)">
          编辑
        </el-button>
        <el-button v-auth="'dict_data_del'" type="danger" link :icon="Delete" @click="deleteAdminUser(scope.row)">
          删除
        </el-button>
      </template>
    </ProTable>
    <DictDataEdit ref="drawerRef" />
  </div>
</template>
<script setup lang="tsx" name="dictType">
import { ref, reactive } from "vue";
import { Support } from "@/api/interface";
import ProTable from "@/components/ProTable/index.vue";
import DictDataEdit from "@/views/support/dictData/DictDataEdit.vue";
import { ProTableInstance, ColumnProps } from "@/components/ProTable/interface";
import { CirclePlus, Delete, EditPen } from "@element-plus/icons-vue";
import { getDictDataList, saveDictData, delDictData } from "@/api/modules/support";
import { useHandleData } from "@/hooks/useHandleData";
import { useRoute } from "vue-router";
const route = useRoute();
const initParam = reactive({ dictType: route.params.dictType });
// ProTable 实例
const proTable = ref<ProTableInstance>();

// 表格配置项
const columns = reactive<ColumnProps<Support.DictData>[]>([
  { prop: "dictLabel", label: "字典值名称" },
  { prop: "dictValue", label: "字典值" },
  { prop: "dictSort", label: "排序" },
  { prop: "remark", label: "备注" },
  { prop: "createdAt", label: "创建时间" },
  { prop: "updatedAt", label: "更新时间" },
  { prop: "operation", label: "操作", width: 250, fixed: "right" }
]);

// 删除权限资源
const deleteAdminUser = async (params: Support.DictData) => {
  await useHandleData(delDictData, { ids: params.dictCode }, `删除【${params.dictLabel}】字典值`);
  proTable.value?.getTableList();
};

// 打开 drawer(新增、查看、编辑)
const drawerRef = ref<InstanceType<typeof DictDataEdit> | null>(null);
const openDrawer = (title: string, row: Partial<Support.DictData> = {}) => {
  const params = {
    title,
    isView: title === "查看",
    row: { ...row },
    api: saveDictData,
    getTableList: proTable.value?.getTableList,
    dictType: initParam.dictType
  };
  drawerRef.value?.acceptParams(params);
};
</script>
